package com.uchengwang.ui.cm_ui.umsg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.db.manager.LiteOrmDBUtil;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.umsg.bean.MsgTopBean;
import com.uchengwang.ui.cm_ui.uu.bean.XiangliaoBean;
import com.uchengwang.ui.cm_ui.uu.http.XiangliaoManager;
import com.uchengwang.ui.cm_ui.uu.ui.AddFriendActivity;
import com.uchengwang.ui.cm_ui.uu.ui.XiangliaoActivity;
import com.uchengwang.ui.cm_ui.uu.ui.XiangliaoChooseMoodActivity;
import com.uchengwang.ui.utils.ToastManager;
import com.uchengwang.ui.utils.UCommonUtil;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/3/21.
 * msg界面顶部的功能
 */

public class MsgTopAdapter extends BaseAdapter{
    private Context context;
    private List<MsgTopBean> list = new ArrayList<>();
    public MsgTopAdapter(Context context, List<MsgTopBean> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        MsgTopVH holder = null;
        if (view == null){
            holder = new MsgTopVH();
            view = LayoutInflater.from(context).inflate(R.layout.layout_msg_top, parent, false);

            holder.ln = view.findViewById(R.id.msgTop_ln);
            holder.imageView = view.findViewById(R.id.msgTop_image);
            holder.textView = view.findViewById(R.id.msgTop_tv);

            view.setTag(holder);
            AutoUtils.auto(view);
        }else {
            holder = (MsgTopVH) view.getTag();
        }

        final MsgTopBean bean = list.get(position);
        holder.textView.setText(bean.getTitle());
        ImageManager.getInstance().loadImage(context, holder.imageView, bean.getResId(), ImageManager.ImageType.NARMOL);
        holder.ln.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                if (UCommonUtil.getUtil().isNeedLogin((Activity) context, false)){
                    return;
                }
                switch (bean.getTitle()){
                    case "享聊"://享聊
                        XiangliaoManager.getManager().chatCheck((Activity) context, new OnResultListener() {
                            @Override
                            public void onResult(int stutas, String msg, int total, String data) {
                                if (stutas ==0){
                                    //可以进入享聊
                                    List<XiangliaoBean> beanList = LiteOrmDBUtil.getLiteOrm(context).getQueryByWhere(XiangliaoBean.class, "uid", String.valueOf(UCommonUtil.getUtil().getId()));
                                    XiangliaoBean xiangliaoBean = null;
                                    if (beanList != null && beanList.size() >0){
                                        xiangliaoBean = beanList.get(0);
                                    }

                                    Intent in = null;
                                    if (xiangliaoBean == null || ((System.currentTimeMillis()-xiangliaoBean.getCreateTime()) >12*60*60*1000)){
                                        in = new Intent(context, XiangliaoChooseMoodActivity.class);
                                    }else {
                                        in = new Intent(context, XiangliaoActivity.class);
                                    }
                                    context.startActivity(in);

                                }else {
//                                    UCommonUtil.getUtil().showToast(context, msg);

                                    ToastManager.getManager().customToast(context, "享聊-身边的心情陪伴", msg);
                                }
                            }
                        });

                        MobclickAgent.onEvent(context,"xiangLiao");
                        break;
                    case "享玩":
                        intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                        intent.putExtra(BaseConfig.URL_TITLE, "享玩");
                        intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/plays");
                        intent.putExtra(BaseConfig.IS_HIDE_RIGHT, true);
                        MobclickAgent.onEvent(context,"xiangWan");
                        break;

                    case "会场":
                        intent = new Intent(context, LoadCycleWebHtmlActivity.class);
                        intent.putExtra(BaseConfig.PUB_TYPE, 2);
                        intent.putExtra(BaseConfig.URL_TITLE, "会场");
                        intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL+"/events");
                        MobclickAgent.onEvent(context,"huiChang");
                        break;

                    case "找同学"://找同学
                        intent = new Intent(context, AddFriendActivity.class);
                        intent.putExtra(BaseConfig.USER_ROLE, BaseConfig.STUDENT_ROLE);
                        intent.putExtra(BaseConfig.TYPE, 2);
                        break;
                    case "找社团"://找社团
                        intent = new Intent(context, AddFriendActivity.class);
                        intent.putExtra(BaseConfig.USER_ROLE, BaseConfig.CLUB_ROLE);
                        intent.putExtra(BaseConfig.TYPE, 3);
                        break;
                    case "找赞助"://找社团
                        UCommonUtil.getUtil().showToast(context, "开通中");
                        break;
                }
                if (intent != null){
                    context.startActivity(intent);
                }
            }
        });

        return view;
    }

    private  class MsgTopVH {
        private LinearLayout ln;
        private ImageView imageView;
        private TextView textView;
    }
}
